package com.hfjyz.system.user.dao;

import java.util.List;

import com.hfjyz.base.bean.SearchCondition;
import com.hfjyz.base.dao.BaseDao;
import com.hfjyz.base.dao.PagingHibernateCallback;
import com.hfjyz.base.entity.MenuEntity;
import com.hfjyz.base.entity.UserEntity;

public class UserDao extends BaseDao<UserEntity> {

    public UserDao() {
        super(UserEntity.class);
    }

    @SuppressWarnings("unchecked")
    public UserEntity findUserById(String userId) {
        String hql = "from UserEntity user where user.userId=?";
        List<UserEntity> entityList = (List<UserEntity>) hibernateTemplate
                .find(hql, userId);
        if (entityList == null || entityList.isEmpty()) {
            return null;
        }

        return entityList.get(0);
    }

    @SuppressWarnings("unchecked")
    public List<MenuEntity> findMenuListByUser(UserEntity user) {
        String hql = "select auth.menu from AuthEntity auth where auth.depart.id=? order by auth.menu.id";
        List<MenuEntity> menuList = (List<MenuEntity>) hibernateTemplate.find(
                hql, user.getDepart().getId());

        return menuList;
    }
    
    @SuppressWarnings("unchecked")
    public List<MenuEntity> findAllMenu() {
        String hql = "from MenuEntity";
        List<MenuEntity> menuList = (List<MenuEntity>) hibernateTemplate.find(hql);
        
        return menuList;
    }

    @SuppressWarnings("unchecked")
    public List<UserEntity> findUser(SearchCondition cond) {
        String hql = "from UserEntity" + cond.getOrderString();
        PagingHibernateCallback callback = new PagingHibernateCallback(hql, cond.getPageNum(),
                cond.getRowCountPerPage());
        List<UserEntity> result = hibernateTemplate.executeFind(callback);
        return result;
    }

    @SuppressWarnings("unchecked")
    public long countUser(SearchCondition searchCondition) {
        String hql = "select count(user.id) from UserEntity as user";
        List<Object> result = hibernateTemplate.find(hql);

        return (Long) result.get(0);
    }
}
